﻿using Himp.Outpatient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace Himp.Outpatient
{
    /// <summary>
    /// 门诊排班记录服务
    /// </summary>
    public class OtpSchdRecService : OutpatientAppService, IOtpSchdRecService
    {
        private readonly IRepository<OtpSchdRec, Guid> _optSchdRecRepository;

        public OtpSchdRecService(IRepository<OtpSchdRec, Guid> optSchdRecRepository)
        {
            _optSchdRecRepository = optSchdRecRepository;
        }

        /// <summary>
        /// 获取门诊排班记录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<OtpSchdRecDto>> GetListAsync(OtpSchdRecInput input)
        {
            var iQuery = await _optSchdRecRepository.WithDetailsAsync();
            //iQuery = iQuery.WhereIf(input.SchdBegndata != null, i => i.SchdDate.Date >= input.SchdBegndata.Value.Date);
            //iQuery = iQuery.WhereIf(input.SchdEnddata != null, i => i.SchdDate.Date <= input.SchdEnddata.Value.Date);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<OtpSchdRec>, List<OtpSchdRecDto>>(iQuery.ToList());

                return new PagedResultDto<OtpSchdRecDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<OtpSchdRec>, List<OtpSchdRecDto>>(items.ToList());

                return new PagedResultDto<OtpSchdRecDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<OtpSchdRecDto> GetAsync(Guid id)
        {
            var optSchdRe = await _optSchdRecRepository.GetAsync(id);
            var itemDto = ObjectMapper.Map<OtpSchdRec, OtpSchdRecDto>(optSchdRe);

            return itemDto;
        }
    }
}
